<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Audit Application List</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/css/lightbox.min.css">
    <style>.layui-table {
        table-layout: auto; /* 自动调整表格布局 */
        width: auto; /* 自动适应内容宽度 */
    }

    .layui-table-cell {
        white-space: nowrap; /* 防止内容换行 */
    }
    </style>
    <script src="../../echarts.js"></script>
</head>
<body>
<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">审核</label>
                    <div class="layui-input-inline">
                        <select name="auditStatus">
                            <option value=0>未审核</option>
                            <option value=1>审核通过</option>
                            <option value=2>审核拒绝</option>
                            <option value=4>查看所有</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>




<div class="layui-container1">

    <div class="layui-col-xs8">

        <table class="layui-table" id="mytable" lay-filter="auditTable"></table>
    </div>

    <div class="layui-col-xs4">
        <div id="main" style="width: 500px;height:400px;"></div>
    </div>
</div>

<!-- 审核弹窗 -->
<div id="auditPopup" style="display: none; padding: 20px;">
    <table class="layui-table1">
        <colgroup>
            <col width="100">
            <col width="200">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>身份信息</th>
            <th>资质证明</th>

        </tr>
        </thead>
        <tbody id="auditInfo"></tbody>
    </table>

</div>

<script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.6.8/layui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.3/js/lightbox.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

<script>
    var myChart = echarts.init(document.getElementById('main'));
    var count0;
    var count1;
    var count2;
    axios.get('http://localhost:8080/audit/getChangePie', {
        withCredentials: true

    }).then(function(res) {
        var data = res.data.data; // 获取auditForm数组

        count0 = data.status0;
        count1 = data.status1;
        count2 = data.status2;

        console.log(count0, count1);

        // 指定图表的配置项和数据
        // 指定图表的配置项和数据
        var option = {
            series: [
                {
                    type: 'pie',
                    radius: '60%',
                    data: [
                        {
                            value: count0,
                            name: '未审核',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside'
                            }
                        },
                        {
                            value: count1,
                            name: '通过',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside'
                            }
                        },
                        {
                            value: count2,
                            name: '拒绝',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside'
                            }
                        }
                    ]
                }
            ]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

    })
    function renderPieChart(count0, count1,count2) {
        // 指定图表的配置项和数据
        var option = {
            series: [
                {
                    type: 'pie',
                    radius: '60%',
                    data: [
                        {
                            value: count0,
                            name: '未审核',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside',
                                rich: {
                                    bold: {
                                        fontWeight: 'bold',
                                        fontSize: 14
                                    }
                                },
                                textStyle: {
                                    rich: {
                                        bold: {}
                                    }
                                }
                            }
                        },
                        {
                            value: count1,
                            name: '通过',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside',
                                rich: {
                                    bold: {
                                        fontWeight: 'bold',
                                        fontSize: 14
                                    }
                                },
                                textStyle: {
                                    rich: {
                                        bold: {}
                                    }
                                }
                            }
                        },
                        {
                            value: count2,
                            name: '拒绝',
                            label: {
                                show: true,
                                formatter: '{b}: {c}个',
                                position: 'outside',
                                rich: {
                                    bold: {
                                        fontWeight: 'bold',
                                        fontSize: 14
                                    }
                                },
                                textStyle: {
                                    rich: {
                                        bold: {}
                                    }
                                }
                            }
                        }
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
    layui.use(['table', 'layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;



        // 自定义列模板：审核状态
        table.set({
            templet: {
                statusTpl: function(d){
                    if(d.auditStatus === 0){
                        return '<span style="color: red;">未审核</span>';
                    } else if(d.auditStatus === 1){
                        return '<span style="color: green;">审核通过</span>';
                    }
                    return '';
                }
            }
        });

        // 表格渲染
        table.render({
            elem: '.layui-table',
            url: 'http://localhost:8080/audit/getUnhandleChanges',
            method: 'GET',
            page: true,
            limit: 10,
            cols: [[
                {field: 'changeId', title: '更正申请编号',hide: true},
                {field: 'engineerId', title: '申请工程师ID',hide: true},
                {field: 'changePhone', title: '电话'},
                {field: 'changeDescription', title: '描述'},
                {field: 'changeProvince', title: '省份'},
                {field: 'changeCity', title: '市区'},
                {field: 'changeArea', title: '区县'},
                {field: 'changeReason', title: '理由'},
                {fixed: 'right', title:'操作', toolbar: '#operationBar'}
            ]]
        });
// 监听表单提交事件
        form.on('submit(data-search-btn)', function(data) {
            // 获取表单输入的值

            data = data.field;


            // var requestData = {
            //     auditName: name,
            //     auditPhone: phone,
            //     auditStatus: status
            // };
            console.log(data)

            // 重新加载表格数据
            table.reload('mytable', {
                url: 'http://localhost:8080/audit/findChanges', // 重新加载数据的 URL
                method: 'POST',
                where: data,
                page: { // 指定页码和每页显示的数据条数
                    curr: 1,
                    limit: 10
                },
                done: function(res, curr, count) {
                    // 重新加载完成后的回调函数
                    console.log('重新加载完成');
                }
            });
            // var tableData = layui.table.cache['mytable'];
            // console.log(tableData);
            return false;
        });


        // 监听表格行工具栏事件
        table.on('tool(auditTable)', function(obj){
            var data = obj.data;
            var changeId = data.changeId;
            if(obj.event === 'detail'){
                // 弹出审核窗口
                layer.confirm('请审核', {
                    btn: ['通过', '拒绝'], // 自定义按钮文本
                    shadeClose: true
                }, function(index) {
                    // 在这里执行接单的逻辑

                    axios.get('http://localhost:8080/audit/handleChange/'+changeId+'/'+1, {
                        withCredentials: true

                    }).then(function(response){
                        layer.msg('审核通过', { time: 400 }, function() {
                            // 提示信息展示完毕后延迟执行页面刷新操作

                            setTimeout(function() {
                                var tableData = layui.table.cache['mytable'];
                                var dataIndex = -1;
                                for (var i = 0; i < tableData.length; i++) {
                                    if (tableData[i].changeId === changeId) {
                                        dataIndex = i;
                                        break;
                                    }
                                }
                                if (dataIndex !== -1) {
                                    // 更新数据项的状态为审核通过
                                    tableData[dataIndex].changeStatus = 1;

                                    // 使用reload方法刷新表格数据
                                    table.reload('mytable', {
                                        data: tableData
                                    });
                                    count1 +=1;
                                    count0-=1;
                                    renderPieChart(count0, count1,count2);
                                }
                            }, 400); // 延迟 1 秒钟刷新页面
                        });
                        layer.close(index);
                    }).catch(function(error) {
                        // 请求失败的处理逻辑
                        console.error(error);
                    });


                    layer.msg("变更审核已完成")
                    layer.close(index); // 关闭确认窗口
                },function (index){
                    layer.prompt({title: '请输入拒绝理由', formType: 2}, function(value, promptIndex) {
                        // 输入框中有内容时执行下面的操作
                        if (value.trim() !== '') {

                            axios.get('http://localhost:8080/audit/handleChange/'+changeId+'/'+2, {
                                withCredentials: true

                            }).then(function(res) {
                                layer.msg('审核完成', { time: 400 }, function() {
                                    // 提示信息展示完毕后延迟执行页面刷新操作
                                    setTimeout(function() {
                                        var tableData = layui.table.cache['mytable'];
                                        var dataIndex = -1;
                                        for (var i = 0; i < tableData.length; i++) {
                                            if (tableData[i].changeId === changeId) {
                                                dataIndex = i;
                                                break;
                                            }
                                        }

                                        if (dataIndex !== -1) {
                                            // 更新数据项的状态为审核通过
                                            tableData[dataIndex].changeStatus = 2;

                                            // 使用reload方法刷新表格数据
                                            table.reload('mytable', {
                                                data: tableData
                                            });
                                            count2 +=1;
                                            count0-=1;
                                            renderPieChart(count0, count1, count2);
                                        }

                                    }, 400); // 延迟 1 秒钟刷新页面
                                });

                            })
                            layer.close(index);
                            layer.close(promptIndex);
                        }
                    });

                });


            }

        });
    });
</script>

<!-- 自定义操作工具栏模板 -->

<script type="text/html" id="statusTpl">
    {{# if(d.changeStatus === 0){ }}
    <i class="layui-icon layui-icon-friends" style="color: dodgerblue;"></i>
    {{# } else if(d.changeStatus === 1){ }}
    <i class="layui-icon layui-icon-ok" style="color: green;"></i>
    {{# }else{ }}
    <i class="layui-icon layui-icon-close" style="color: red;"></i>
    {{# } }}
</script>

<script type="text/html" id="operationBar">
    {{# if(d.changeStatus === 1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    {{# } else if(d.changeStatus === 0){ }}
    <a style="background-color: #16baaa; color:white" class="layui-btn layui-btn-primary  layui-btn-xs"  lay-event="detail">审核</a>
    {{# } else{ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    {{# } }}
</script>

<script>
    $(document).ready(function() {
        lightbox.option({
            'resizeDuration': 200,
            'wrapAround': true,
            'positionFromTop': 100
        });
    });
</script>

</body>
</html>
